<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="UTF-8" />
<title>客户管理</title>
<link rel="stylesheet" type="text/css" href="../Public/jquery-easyui-1.3.4/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="../Public/jquery-easyui-1.3.4/themes/icon.css">
<script type="text/javascript" src="../Public/jquery-easyui-1.3.4/jquery.min.js"></script>
<script type="text/javascript" src="../Public/jquery-easyui-1.3.4/jquery.easyui.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
	$('#mytable').datagrid({
		nowrap: false,
		striped: true,
		url:'index.php?s=/Customer/indexjson',
		loadMsg:'数据正在加载，请稍候...',
		columns:[[
			{field:'table_id',checkbox:true},
			{field:'table_title',title:'客户名称',width:200,align:"left"},
			{field:'table_shorttitle',title:'客户简称',width:100,align:"left"},
			{field:'table_address',title:'地址',width:150,align:"left"},
			{field:'table_province',title:'省份',width:50,align:"left",formatter:function(value){
				return value.name;
			}},
			{field:'table_contact',title:'联系人',width:50,align:"left"},
			{field:'table_contacts_number',title:'联系人数量',width:60,align:"left"},
			{field:'table_phone',title:'电话',width:100,align:"left"},
			{field:'table_mobile',title:'手机',width:100,align:"left"},
			{field:'table_email',title:'Email',width:100,align:"left"},
			{field:'table_typeid',title:'客户类型',width:50,align:"left",formatter:function(value){
				return value.name;
			}},
			{field:'table_cust_special',title:'客户属性',width:80,align:"center",formatter:function(value){
				if(value=='0'){
					return '无';
				}else if(value=='1'){
					return '框架内';
				
				}
			}},
			{field:'table_frame_ratio',title:'框架比例（渠道）',width:50,align:"left"},
			{field:'table_frame_ratio2',title:'框架比例（大客）',width:50,align:"left"},
			{field:'table_status',title:'状态',width:50,align:"left",formatter:function(value){
				if(value=='1'){
					return '<font color=red>删除</font>';
				}else{
					return '正常';
				}
			}}
		]],
		singleSelect:true,
		fit:true,
		pagination:true,
		pageSize:20,
		toolbar:[{
			text:'新增客户',
			iconCls:'icon-add',
			handler:addAction
		},'-',{
			text:'编辑客户',
			iconCls:'icon-edit',
			handler:editAction
		},'-',{
			text:'新增联系人',
			iconCls:'icon-add',
			handler:showContactsWindow
		},'-',{
			text:'删除客户',
			iconCls:'icon-remove',
			handler:removeAction
		}]
	});
	
	$('#form_cust_special').bind("change", function(){
		// 去掉框架比例表单的禁用功能
		if ($('#form_cust_special').val() == 1)
		{ 
			$('#form_frame_ratio').removeAttr("disabled"); 
			$('#form_frame_ratio2').removeAttr("disabled"); 
		}
		else
		{
			$('#form_frame_ratio').val('');
			$('#form_frame_ratio').attr("disabled","disabled");
			
			$('#form_frame_ratio2').val('');
			$('#form_frame_ratio2').attr("disabled","disabled");
		}
	});
});
function addAction(){
	$("#form_title").val('');
	$("#form_shorttitle").val('');
	//$("#form_address").val('');
	$("#form_province").val('0');
	//$("#form_contact").val('');
	//$("#form_phone").val('');
	//$("#form_mobile").val('');
	//$("#form_email").val('');
	$("#form_cust_special").val('');
	$("#form_frame_ratio").val('');
	$("#form_frame_ratio").attr("disabled","disabled");
	
	$("#form_frame_ratio2").val('');
	$("#form_frame_ratio2").attr("disabled","disabled");
	$("#form_typeid").val('0');
	$("#formWindow").window('setTitle','新增客户');
	$("#submit").unbind("click");
	$("#submit").bind("click",{type:"add"},checkSubmit);
	$("#formWindow").window('open');
	$("#myLayout").layout('resize');
}
function editAction(){
	var selected = $('#mytable').datagrid('getSelected');
	if(!selected){
		$.messager.alert('错误消息','请先选择一个客户再编辑！','error');
	}else if(selected.table_status=='1'){
		$.messager.alert('错误消息','该客户已经被删除，不能再修改！','error');
	}else{
		$("#form_title").val(selected.table_title);
		$("#form_shorttitle").val(selected.table_shorttitle);
		//$("#form_address").val(selected.table_address);
		$("#form_province").val(selected.table_province.id);
		//$("#form_contact").val(selected.table_contact);
		//$("#form_phone").val(selected.table_phone);
		//$("#form_mobile").val(selected.table_mobile);
		//$("#form_email").val(selected.table_email);
		//alert(selected.table_cust_special);
		if (selected.table_cust_special == '0') // 1为框架内
		{ 
			$("#form_frame_ratio").attr("disabled","disabled"); 
			$("#form_frame_ratio2").attr("disabled","disabled"); 
		}
		else
		{ 
			$("#form_frame_ratio").removeAttr("disabled");			
			$("#form_frame_ratio2").removeAttr("disabled");			
		}
		
		$("#form_frame_ratio").val(selected.table_frame_ratio);
		$("#form_frame_ratio2").val(selected.table_frame_ratio2);
		
		$("#form_cust_special").val(selected.table_cust_special);
		$("#form_typeid").val(selected.table_typeid.id);
		$("#formWindow").window('setTitle','编辑客户');
		$("#submit").unbind("click");
		$("#submit").bind("click",{type:"edit",table_id:selected.table_id},checkSubmit);
		$("#formWindow").window('open');
		$("#myLayout").layout('resize');
	}
}
function removeAction(){
	var selected = $('#mytable').datagrid('getSelected');
	if(!selected){
		$.messager.alert('错误消息','请先选择一个客户再删除！','error');
	}else if(selected.table_status=='1'){
		$.messager.alert('错误消息','该客户已经被删除！','error');
	}else{
		$.messager.confirm('提示消息','确认删除？',function(r){
			if(r){
				$.post(window.location.href,'submit=remove&form_id='+selected.table_id,function(data){
					if(data.status=='false'){
						$.messager.alert('错误消息',data.msg,'error');
					}else{
						$.messager.alert('提示消息',data.msg,'info',function(){
							$("#mytable").datagrid('reload');
						});
					}
				},"json");
			}
		});
	}
}
function closeForm(){
	$("#formWindow").window('close');
}
function checkSubmit(event){
	var $ratio = parseInt($('#form_frame_ratio').val());
	var $ratio2 = parseInt($('#form_frame_ratio2').val());
	
	
	if(!$("#myForm").form('validate')){
		$.messager.alert('错误消息','请确保表单信息无误再提交！','error');
	}
	else if($('#form_cust_special').val() == 1 && $.trim($('#form_frame_ratio').val()) == '')
	{
		$.messager.alert('错误消息','框架内公司必须填写框架比例！','error');
	}
	else if ($.trim($('#form_frame_ratio').val()) != '' && ($ratio > 100 || $ratio < 0))
	{
		$.messager.alert('错误消息','框架比例（渠道）格式错误！','error'); 
	}
	else if ($.trim($('#form_frame_ratio2').val()) != '' && ($ratio2 > 100 || $ratio2 < 0))
	{
		$.messager.alert('错误消息','框架比例（大客）格式错误！','error'); 
	}
	else{
		closeForm();
		$.post(window.location.href,$('#myForm').serialize()+'&submit='+event.data.type+'&form_id='+event.data.table_id,function(data){
			if(data.status=='false'){
				$.messager.alert('错误消息',data.msg,'error');
			}else{
				$.messager.alert('提示消息',data.msg,'info',function(){
					$("#mytable").datagrid('reload');
				});
			}
		},"json"); 
	}
}
function searchres(){
	var searchtitle = encodeURIComponent($.trim($("#searchtitle").val()));
	var searchaddress = encodeURIComponent($.trim($("#searchaddress").val()));
	var searchprovince = $("#searchprovince").val();
	var searchcontact = encodeURIComponent($.trim($("#searchcontact").val()));
	var searchphone = encodeURIComponent($.trim($("#searchphone").val()));
	var searchtypeid = $("#searchtypeid").val();	$('#mytable').datagrid({pageNumber:'1',pageSize:'20',url:'ajax.php?act=customer&searchtitle='+searchtitle+'&searchaddress='+searchaddress+'&searchprovince='+searchprovince+'&searchcontact='+searchcontact+'&searchphone='+searchphone+'&searchtypeid='+searchtypeid});
}
function undo(){
	$("#searchtitle").val('');
	$("#searchaddress").val('');
	$("#searchprovince").val('0');
	$("#searchcontact").val('');
	$("#searchphone").val('');
	$("#searchtypeid").val('0');
}

// 显示联系人弹出框
function showContactsWindow()
{
	var selected = $('#mytable').datagrid('getSelected');
	
	if(!selected){
		$.messager.alert('错误消息','请先选择一个客户再操作！','error');
	}else if(selected.table_status=='1'){
		$.messager.alert('错误消息','该客户已经被删除，不能再操作！','error');
	}else{
		$("#contact").val('');
		$("#telephone").val('');
		$("#phone").val('');
		$("#email").val('');
		$("#company_name").val('');
		$("#company_short").val('');
		$("#company_address").val('');
		$("#province").val('0');
		
		$("#contactsForm").window('open');
		$("#submitForm").unbind("click");
		$("#submitForm").bind("click",{type:"addContacts",table_id:selected.table_id},checkInfo);
	}
}

// 验证联系人表单
function checkInfo(event)
{
	if(!$("#contacts_form").form('validate')){
		$.messager.alert('错误消息','请确保表单信息无误再提交！','error');
	}else{
		closeWindow('#contactsForm');
		$.post(window.location.href,$('#contacts_form').serialize()+'&submit='+event.data.type+'&form_id='+event.data.table_id,function(data){
			if(data.status=='false'){
				$.messager.alert('错误消息',data.msg,'error');
			}else{
				$.messager.alert('提示消息',data.msg,'info',function(){
					$("#mytable").datagrid('reload');
				});
			}
		},"json"); 
	}
}

// 关闭弹窗
function closeWindow(selector)
{
	$(selector).window('close');
}
</script>
</head>
<body class="easyui-layout">
<div region="center" title="客户设置-&gt;客户管理">
<div class="easyui-layout" fit="true">
	<div region="north" border="false" style="height:70px;padding:2px;padding-left:5px;background-color:#DFE8F6;font-size:11px;color:#15428B;"><table><tr><td>客户名称/简称：</td><td><INPUT TYPE="text" NAME="searchtitle" id="searchtitle" style="width:150px;"></td><td>地址：</td><td><INPUT TYPE="text" NAME="searchaddress" id="searchaddress" style="width:150px;"></td><td>省份：</td><td><SELECT NAME="searchprovince" id="searchprovince" style="width:120px"><option value="0">──请选择──</option><option value="1">北京市</option><option value="2">上海市</option><option value="3">天津市</option><option value="4">重庆市</option><option value="5">黑龙江省</option><option value="6">辽宁省</option><option value="7">吉林省</option><option value="8">河北省</option><option value="9">内蒙古</option><option value="10">陕西省</option><option value="11">山西省</option><option value="12">甘肃省</option><option value="13">宁夏省</option><option value="14">新疆省</option><option value="15">西藏省</option><option value="16">青海省</option><option value="17">四川省</option><option value="18">云南省</option><option value="19">贵州省</option><option value="20">湖南省</option><option value="21">湖北省</option><option value="22">河南省</option><option value="23">山东省</option><option value="24">安徽省</option><option value="25">江苏省</option><option value="26">浙江省</option><option value="30">广东省</option><option value="31">广西省</option><option value="32">江西省</option><option value="33">福建省</option><option value="34">海南省</option></SELECT></td><td><a class="easyui-linkbutton" icon="icon-search" onclick="searchres();">搜索</a></td></tr><tr><td>联系人：</td><td><INPUT TYPE="text" NAME="searchcontact" id="searchcontact" style="width:150px;"></td><td>电话/手机/Email：</td><td><INPUT TYPE="text" NAME="searchphone" id="searchphone" style="width:150px;"></td><td>客户类型：</td><td><SELECT NAME="searchtypeid" id="searchtypeid" style="width:120px"><option value="0">──请选择──</option><option value="1">直客</option>,<option value="2">代理公司</option></SELECT></td><td><a class="easyui-linkbutton" icon="icon-undo" onclick="undo();">重置</a></td></tr></table></div>
	<div region="center" border="true"><table id="mytable"></table></div>
</div>
</div>
<div id="formWindow" class="easyui-window" title="新增客户" minimizable="false" collapsible="false" maximizable="false" closed="true" modal="true" style="width:450px;height:400px;padding:20px;">
	<div class="easyui-layout" fit="true" id="myLayout">
		<div region="center" border="false">
		<form id="myForm" method="post">
			<table>
				<tr><td>客户名称：</td><td><INPUT class="easyui-validatebox" required="true" missingMessage="客户名称不能为空" TYPE="text" NAME="form_title" id="form_title" style="width:250px"></td></tr>
				<tr><td>客户简称：</td><td><INPUT TYPE="text" NAME="form_shorttitle" id="form_shorttitle" style="width:250px"></td></tr>
				<!--<tr><td>地址：</td><td><INPUT TYPE="text" NAME="form_address" id="form_address" style="width:250px"></td></tr>-->
				<tr><td>省份：</td><td><SELECT NAME="form_province" id="form_province" style="width:250px"><option value="0">────请选择省份────</option><option value="1">北京市</option><option value="2">上海市</option><option value="3">天津市</option><option value="4">重庆市</option><option value="5">黑龙江省</option><option value="6">辽宁省</option><option value="7">吉林省</option><option value="8">河北省</option><option value="9">内蒙古</option><option value="10">陕西省</option><option value="11">山西省</option><option value="12">甘肃省</option><option value="13">宁夏省</option><option value="14">新疆省</option><option value="15">西藏省</option><option value="16">青海省</option><option value="17">四川省</option><option value="18">云南省</option><option value="19">贵州省</option><option value="20">湖南省</option><option value="21">湖北省</option><option value="22">河南省</option><option value="23">山东省</option><option value="24">安徽省</option><option value="25">江苏省</option><option value="26">浙江省</option><option value="30">广东省</option><option value="31">广西省</option><option value="32">江西省</option><option value="33">福建省</option><option value="34">海南省</option></SELECT></td></tr>
				<!--<tr><td>联系人：</td><td><INPUT TYPE="text" NAME="form_contact" id="form_contact" style="width:250px"></td></tr>-->
				<!--<tr><td>电话：</td><td><INPUT TYPE="text" NAME="form_phone" id="form_phone" style="width:250px"></td></tr>-->
				<!--<tr><td>手机：</td><td><INPUT TYPE="text" NAME="form_mobile" id="form_mobile" style="width:250px"></td></tr>-->
				<!--<tr><td>Email：</td><td><INPUT class="easyui-validatebox" validType="email" TYPE="text" NAME="form_email" id="form_email" style="width:250px"></td></tr>-->
				<tr><td>客户类型：</td><td><SELECT NAME="form_typeid" id="form_typeid" style="width:250px"><option value="0">────请选择客户类型────</option><option value="1">直客</option>,<option value="2">代理公司</option></SELECT></td></tr>
				<tr><td>客户属性：</td><td><SELECT   class="easyui-validatebox"    NAME="form_cust_special" id="form_cust_special" style="width:250px"   >
				<option value="0">─无─</option> 
				<option value="1">─框架内─</option>
				</SELECT></td></tr>
				<tr><td>框架比例（渠道）</td><td><input type="text" disabled="disabled" name="form_frame_ratio" id="form_frame_ratio" style="width:250px" />&nbsp;%</td></tr>
				<tr><td>框架比例（大客）</td><td><input type="text" disabled="disabled" name="form_frame_ratio2" id="form_frame_ratio2" style="width:250px" />&nbsp;%</td></tr>
			</table>
		</form>
		</div>
		<div region="south" border="false" style="text-align:right;height:30px;line-height:30px;">
			<a class="easyui-linkbutton" icon="icon-ok" href="javascript:void(0)" id="submit">确定</a>
			<a class="easyui-linkbutton" icon="icon-cancel" href="javascript:void(0)" onclick="closeForm()">取消</a>
		</div>
	</div>
</div>

<!-- 添加联系人用到的弹出框 -->
<div id="contactsForm" class="easyui-window" title="新增联系人" minimizable="false" collapsible="false" maximizable="false" closed="true" modal="true" style="width:450px;height:400px;padding:20px;">
	<div class="easyui-layout" fit="true" id="myLayout">
		<div region="center" border="false">
		<form id="contacts_form" method="post">
			<table>
				<tr><td>联系人：</td><td><input type="text" name="form[contact]" id="contact" style="width:250px"></td></tr>
				<tr><td>电话：</td><td><input type="text" name="form[telephone]" id="telephone" style="width:250px"></td></tr>
				<tr><td>手机：</td><td><input type="text" name="form[phone]" id="phone" style="width:250px"></td></tr>
				<tr><td>Email：</td><td><input class="easyui-validatebox" validType="email" type="text" name="form[email]" id="email" style="width:250px"></td></tr>
				<tr><td>邮编：</td><td><input style="width:250px" type="text" name="form[postcode]" /></td></tr>
				<tr><td>传真：</td><td><input style="width:250px" type="text" name="form[fax]" /></td></tr>
				<tr><td>省份：</td><td><select name="form[province]" id="province" style="width:250px"><option value="0">────请选择省份────</option><option value="1">北京市</option><option value="2">上海市</option><option value="3">天津市</option><option value="4">重庆市</option><option value="5">黑龙江省</option><option value="6">辽宁省</option><option value="7">吉林省</option><option value="8">河北省</option><option value="9">内蒙古</option><option value="10">陕西省</option><option value="11">山西省</option><option value="12">甘肃省</option><option value="13">宁夏省</option><option value="14">新疆省</option><option value="15">西藏省</option><option value="16">青海省</option><option value="17">四川省</option><option value="18">云南省</option><option value="19">贵州省</option><option value="20">湖南省</option><option value="21">湖北省</option><option value="22">河南省</option><option value="23">山东省</option><option value="24">安徽省</option><option value="25">江苏省</option><option value="26">浙江省</option><option value="30">广东省</option><option value="31">广西省</option><option value="32">江西省</option><option value="33">福建省</option><option value="34">海南省</option></select></td></tr>
				<!--<tr><td>分公司名称：</td><td><input class="easyui-validatebox" required="true" missingMessage="客户名称不能为空" type="text" name="form[company_name]" id="company_name" style="width:250px"></td></tr>-->
				<!--<tr><td>分公司简称：</td><td><input type="text" name="form[company_short]" id="company_short" style="width:250px"></td></tr>-->
				<tr><td>公司地址：</td><td><input type="text" name="form[company_address]" id="company_address" style="width:250px"></td></tr>
			</table>
		</form>
		</div>
		<div region="south" border="false" style="text-align:right;height:30px;line-height:30px;">
			<a class="easyui-linkbutton" icon="icon-ok" href="javascript:void(0)" id="submitForm">确定</a>
			<a class="easyui-linkbutton" icon="icon-cancel" href="javascript:void(0)" onclick="closeWindow('#contactsForm')">取消</a>
		</div>
	</div>
</div>
</body>
</html>